//
//  NSObject+ZXJSONCategory.m
//  MerchantBusinessClient
//
//  Created by 朱新明 on 2020/2/2.
//  Copyright © 2020 com.Chs. All rights reserved.
//

#import "NSObject+ZXJSONCategory.h"
#import "NSString+CAIWhithoutEscaping.h"

@implementation NSObject (ZXJSONCategory)


+ (nullable id)zx_getJSONObjectWithSerializationJSONData:(nullable NSData *)data
{
    if (!data) {
        return nil;
    }
    NSError *error=nil;
    id dic ;
    if (@available(iOS 15.0, *)) {
        dic = [NSJSONSerialization JSONObjectWithData:data
                                               options:NSJSONReadingMutableContainers|NSJSONReadingJSON5Allowed
                                                 error:&error];
    }else {
        dic = [NSJSONSerialization JSONObjectWithData:data
                                                 options:NSJSONReadingMutableContainers
                                                   error:&error];
    }
    if (!error && data)
    {
        return dic;
    }
    return nil;
}

+ (nullable id)zx_getJSONObjectFromSerializationString:(nullable NSString *)string
{
    return [self zx_getJSONObjectFromSerializationJSON:string];
}

+ (nullable id)zx_getJSONObjectFromSerializationJSON:(nullable id)json
{
    NSData *jsonData = nil;
    if (!json || json == (id)kCFNull) {
        return nil;
    }
    if ([json isKindOfClass:[NSNull class]]) {
        return nil;
    }
    if ([json isKindOfClass:[NSDictionary class]] || [json isKindOfClass:[NSArray class]]) {
        return json;
    }
    if ([json isKindOfClass:[NSString class]])
    {
        if ([[(NSString *)json stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0)
        {
            return nil;
        }
        jsonData = [(NSString *)json dataUsingEncoding:NSUTF8StringEncoding];
    }
    else if ([json isKindOfClass:[NSData class]]) {
        jsonData = json;
    }
    return [self zx_getJSONObjectWithSerializationJSONData:jsonData];
}

+ (nullable id)zx_getJSONObjectFromContentsOfFile:(NSString *)path
{
//    NSString *path = [[NSBundle mainBundle]pathForResource:@"ZXPrivacyPolicy" ofType:@"json"];
//    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    id dic = [NSJSONSerialization JSONObjectWithData:data
                                             options:NSJSONReadingMutableContainers
                                               error:&error];
    if (error==nil && data!=nil)
    {
        return dic;
    }
    return nil;

}

+ (nullable NSString *)zx_getJSONStringFromJSONObject:(nullable id)responseObject
{
    if ([NSJSONSerialization isValidJSONObject:responseObject])
    {
        NSError *error = nil;
    
        if (@available(iOS 13.0, *)) {
            NSData *data = [NSJSONSerialization dataWithJSONObject:responseObject
                                                           options:NSJSONWritingSortedKeys|NSJSONWritingFragmentsAllowed|NSJSONWritingWithoutEscapingSlashes
                                                             error:&error];
             if (!error && data!=nil)
             {
                 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                 return str;
             }
            return nil;
        }else{
            NSData *data = [NSJSONSerialization dataWithJSONObject:responseObject
                                                           options:NSJSONWritingFragmentsAllowed
                                                             error:&error];
             if (!error && data!=nil)
             {
                 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                 str = [NSString zx_whitoutEscapingCharacterWithJsonString:str];
                 return str;
             }
        }
    }
    return nil;
}

+ (nullable NSString *)zx_getJSONPrettyPrintedStringFromJSONObject:(nullable id)responseObject
{
    if ([NSJSONSerialization isValidJSONObject:responseObject])
    {
        NSError *error = nil;

        if (@available(iOS 13.0, *)) {
            NSData *data = [NSJSONSerialization dataWithJSONObject:responseObject
                                                           options:NSJSONWritingPrettyPrinted|NSJSONWritingSortedKeys|NSJSONWritingFragmentsAllowed|NSJSONWritingWithoutEscapingSlashes
                                                             error:&error];
             if (!error && data!=nil)
             {
                 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                 return str;
             }
            return nil;
        }else{
            NSData *data = [NSJSONSerialization dataWithJSONObject:responseObject
                                                           options:NSJSONWritingPrettyPrinted|NSJSONWritingFragmentsAllowed
                                                             error:&error];
             if (!error && data!=nil)
             {
                 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                 str = [NSString zx_whitoutEscapingCharacterWithJsonString:str];
                 return str;
             }
        }
    }
    return nil;
}




@end
